﻿using System;
using System.Collections.Generic;
using System.Text;
using GlobalHelper;

namespace Loader
{
    public class MainInfoOperator
    {
        /// <summary>
        /// 获得 IBDM线路编号
        /// </summary>
        /// <param name="lineId"></param>
        /// <returns></returns>
        public static string GetIbdmId(string lineId)
        {
            return lineId;
        }

        /// <summary>
        /// 获取时刻表名称
        /// </summary>
        /// <param name="baseInfo"></param>
        /// <returns></returns>
        public static string GetBusScheduleName(string baseInfo)
        {
            return baseInfo;
        }

        /// <summary>
        /// 获取起效的星期
        /// </summary>
        /// <param name="baseInfo"></param>
        /// <returns>UseSort形式为10001</returns>
        public static string GetUseSort(string baseInfo)
        {
            string result = string.Empty;

            if (IsFullWeek(baseInfo))
            {
                result = Helper.FulWeekUseSort;
            }
            else if (IsMondayToFirday(baseInfo))
            {
                result = Helper.MondayToFirdayUseSort;
            }
            else if (IsSaturdayToSunday(baseInfo))
            {
                result = Helper.SaturdayToSundayUseSort;
            }

            return result;
        }

        /// <summary>
        /// 判断是否 周六至周日
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static bool IsSaturdayToSunday(string s)
        {
            return CheckString(s, Helper.SaturdayToSunday);
        }

        /// <summary>
        /// 判断是否 周一至周五
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static bool IsMondayToFirday(string s)
        {
            return CheckString(s, Helper.MondayToFirday);
        }

        /// <summary>
        /// 判断是否 周一至周日
        /// </summary>
        /// <param name="s"></param>
        /// <returns></returns>
        private static bool IsFullWeek(string s)
        {
            return CheckString(s, Helper.FullWeek);
        }

        /// <summary>
        /// 判断 输入的主串中是否包含
        /// 所提供的子串
        /// </summary>
        /// <param name="baseStr"></param>
        /// <param name="subStrs"></param>
        /// <returns></returns>
        private static bool CheckString(string baseStr, IList<string> subStrs)
        {
            if (string.IsNullOrEmpty(baseStr))
            {
                return false;
            }

            bool result = false;

            IList<string> flagString = subStrs;

            foreach (string flag in flagString)
            {
                if (CheckString(baseStr,flag))
                {
                    result = true;
                    break;
                }
            }

            return result;
        }

        /// <summary>
        /// 判断 输入的主串中是否包含
        /// 所提供的子串
        /// </summary>
        /// <param name="baseStr"></param>
        /// <param name="subStrs"></param>
        /// <returns></returns>
        private static bool CheckString(string baseStr, string subStrs)
        {
            if (string.IsNullOrEmpty(baseStr))
            {
                return false;
            }

            bool result = false;

            if (baseStr.IndexOf(subStrs) != -1)
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 求一组字符串中最长的字符串
        /// </summary>
        /// <param name="strs"></param>
        /// <returns></returns>
        private static string GetLongestString(IList<string> strs)
        {
            if (strs.Count <= 0)
            {
                return string.Empty;
            }

            string result = string.Empty;

            int max = 0;
            int maxPosition = -1;

            for (int i = 0; i < strs.Count; i++)
            {
                if (strs[i].Length > max)
                {
                    max = strs[i].Length;
                    maxPosition = i;
                }
            }

            if (maxPosition != -1)
            {
                result = strs[maxPosition];
            }

            return result;
        }

        /// <summary>
        /// 从时刻表标题中获取 线路名称
        /// </summary>
        /// <param name="busScheduleMainInfo"></param>
        /// <returns>形如:551</returns>
        public static string GetLineName(string busScheduleMainInfo)
        {
            string result = string.Empty;

            //1. 从ITDC获取所有线路名称
            IList<string> allLineName = ITDCGate.DBGate.GetAllLineName();

            //2. 判断串中是否出现线路名称
            IList<string> compare = new List<string>();

            foreach (string lineName in allLineName)
            {
                if (CheckString(busScheduleMainInfo, lineName))
                {
                    compare.Add(lineName);
                }
            }

            //3. 有存在,返回ID
            if (compare.Count > 0)
            {
                result = GetLongestString(compare);
            }

            return result;
        }
    }
}
